package com.mall.evaluate.controller;



import com.github.pagehelper.PageInfo;
import com.mall.common.response.CommonResponse;
import com.mall.constant.enums.CommonStatus;
import com.mall.evaluate.entity.TbEvaluate;
import com.mall.evaluate.service.TbEvaluateService;
import com.mall.evaluate.vo.VoEvaluate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @author  XueYingDan
 * @email  409978619@qq.com
 * @date 2021/7/18 18:43
 * @version 1.0
 */
@RestController
@RequestMapping("/evaluate") // 商品评论
@CrossOrigin
public class TbEvluateController {

    @Autowired
    private TbEvaluateService evalueateService;

    /**
     *  商品评论列表
     * @return
     */
    @GetMapping("/findAll")
    public List<TbEvaluate> findAll(){
        return evalueateService.findAll();
    }

    /**
     * 添加商品评论
     */
    @PostMapping("/addEvaluate")
    public CommonResponse addEvaluate(@RequestBody TbEvaluate evaluate){
        try {
            evalueateService.addEvaluate(evaluate);
            return new CommonResponse(CommonStatus.SUCCESS.getCode(),CommonStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.ERROR.getCode(),CommonStatus.ERROR.getMessage());
        }

    }


    /**
     * 删除商品评论
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public CommonResponse delete(@PathVariable("id") Integer id){
        try {
            evalueateService.delete(id);
            return new CommonResponse(CommonStatus.SUCCESS.getCode(),CommonStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.ERROR.getCode(),CommonStatus.ERROR.getMessage());
        }

    }


    /**
     * 模糊查询
     * @return
     */
    @PostMapping("/findMohu")
    public PageInfo findMohu(@RequestBody TbEvaluate evaluate
            , @RequestParam(value = "page",defaultValue = "1")int page
            , @RequestParam(value = "size",defaultValue ="2" )int size){
        return evalueateService.findMohu(evaluate,page,size);
    }



    /**
     * 回复评价
     */
    @GetMapping("/reply")
    public CommonResponse reply(Integer id, String replyContent){
        try {
            evalueateService.reply(id,replyContent);
            return new CommonResponse(CommonStatus.SUCCESS.getCode(),CommonStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.ERROR.getCode(),CommonStatus.ERROR.getMessage());
        }

    }
}
